import { Get } from './zz_restful'
import Main from '@/components/main'
import store from '@/store'

const getAllMenu = () => {
  return Get('/menu/all')
}
// 获取自己的菜单id
const getOwnMenu = () => {
  return Get('/menu/own')
}

export const RouterMenu = {
  init (router) {
    let localRoutes = router.options.routes
    console.log('------初始化路由')
    if (localRoutes.length > 2) {
      console.log('比较路由')
      getOwnMenu().then(res => {
        let data = res.data.data
        if (data && data.length > 0) {
          localRoutes.forEach(lr => {
            // 下面的解释详见服务端
            //
            if (lr.id && data.some(d => d === lr.id)) lr.meta.hideInMenu = false // 菜单显示
            else lr.meta.hideInMenu = true // 隐藏
            //
            if (lr.children && lr.children.length > 0) {
              lr.children.forEach(lrc => {
                if (lrc.id && data.some(d => d === lrc.id)) {
                  lrc.meta.hideInMenu = false
                  lr.meta.hideInMenu = false
                } else lrc.meta.hideInMenu = true
              })
            }
            /*
            let allChildHide = lr.children.some(d => {
              if (d.meta.hideInMenu === false) return false
              return true
            })
            if (lr.meta.hideInMenu === false && allChildHide) lr.meta.hideInMenu = true
             */
          })
        }
      }).catch(err => console.log('加载自己的菜单id数据错误：', err))
      return
    }
    getAllMenu().then(res => {
      if (res && res.status === 200) {
        // sessionStorage.setItem('menus', JSON.stringify(resp.data))
        InitRouterConfig(router, store, res.data.data || [])
      }
    }).catch(err => console.log('初始化所有菜单数据错误：', err))
  }
}

// 动态添加路由
const InitRouterConfig = (router, store, menus) => {
  console.log('加载路由')
  let frs = BuildParentRoutes(menus)
  router.options.routes.push(...frs)
  router.addRoutes(router.options.routes)
  store.commit('setRoutes', menus)
}

/**
 * 构建路由父节点
 * https://router.vuejs.org/zh/api/#router-构建选项
 * @param menus
 * @returns {Array}
 */
const BuildParentRoutes = (menus) => {
  let parentRoutes = []
  menus.forEach(menu => {
    let {
      id,
      path,
      name,
      meta,
      children
    } = menu
    let pr = {
      id: id,
      path: '/' + path,
      component: Main,
      name: name,
      // icon: icon,
      meta: meta,
      children: children && BuildChildenRoutes(children)
    }
    parentRoutes.push(pr)
  })
  return parentRoutes
}

/**
 * 构建路由子节点
 * @param childs
 * @returns {Array}
 */
const BuildChildenRoutes = (childs) => {
  let result = []
  childs.forEach(child => {
    let {
      id,
      path,
      name,
      modular,
      component,
      meta,
      children
    } = child

    if (children && children instanceof Array) {
      children.forEach(ch => {
        if (ch && ch instanceof Array) {
          children = BuildChildenRoutes(ch)
        }
      })
    }

    let fcr = {
      id: id,
      path: path,
      component: resolve => require(['@/view' + modular + component + '.vue'], resolve),
      name: name,
      meta: meta,
      children: children
    }
    result.push(fcr)
  })
  return result
}
